package com.xxx.client.android.dialog;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

import com.xxx.client.android.R;
import com.xxx.client.android.activity.HomeActivity;
import com.xxx.client.android.api.FeedbackVO;
import com.xxx.client.android.api.WSError;
import com.xxx.client.android.api.XXXApi;
import com.xxx.client.android.api.impl.XXXApiImpl;
import com.xxx.client.android.util.Tool;

public class FeedbackLoadingDialog extends LoadingDialog<String, Boolean> {

	public FeedbackLoadingDialog(Activity activity, int loadingMsg, int failMsg) {
		super(activity, loadingMsg, failMsg);
	}

	@Override
	public Boolean doInBackground(String... params) {

		XXXApi service = new XXXApiImpl();
		Boolean b = false;
		String feedbackContent = params[0];
		try {
			if (feedbackContent != null) {
				FeedbackVO vo = new FeedbackVO();
				PackageManager manager = mActivity.getPackageManager();
				try {
					StringBuffer nameStringBuffer = new StringBuffer();
					nameStringBuffer.append("Android:" + android.os.Build.MODEL
							+ "," + android.os.Build.VERSION.SDK_INT
							+ "  Version:");
					PackageInfo info = manager.getPackageInfo(
							mActivity.getPackageName(), 0);
					nameStringBuffer.append(info.versionName);
					vo.setUserFeedback(nameStringBuffer.toString());
				} catch (NameNotFoundException e) {
					vo.setUserFeedback("");
				}
				vo.setFeedbackContent(feedbackContent);
				b = service.insertFeedbackByObject(vo);
			} else {
				// do nothing
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			return b;
		} catch (WSError e) {
			publishProgress(e);
			cancel(true);
		}
		return b;

	}

	@Override
	public void doStuffWithResult(Boolean b) {

		Intent intent = new Intent(mActivity, HomeActivity.class);

		if (b) {
			mActivity.startActivity(intent);
			Tool.messageBox(mActivity, R.string.feedback_success);
		} else {
			Tool.messageBox(mActivity, R.string.feedback_fail);
		}
	}

}
